/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: bjc
 * Date: 2025-11-03
 * Time: 15:58
 */
public class Test04_maxArea {

    public static int maxArea1(int[] height) {
        int left = 0;
        int right = height.length-1;
        int currentHeight = Math.min(height[left],height[right]);
        int area = (right-left)*currentHeight;
        while (left < right){
            //此时情况就是，只有在左指针比当前高度小的时候，才进行移动
            while (left < right && height[left] <= currentHeight){
                left++;
            }
            while (left < right && height[right] <= currentHeight){
                right--;
            }
            currentHeight = Math.min(height[left],height[right]);
            area = Math.max(area,(right-left)*currentHeight);
        }
        return area;
    }
    public static int maxArea(int[] height) {
        int left = 0;
        int right = height.length-1;
        int result = (right-left)*Math.min(height[left],height[right]);
        int tmp = 0;
        while (left < right){
            if(height[left] < height[right]){
                /*
                 * 分析情况：
                 * 此时容器左高度小于右高度，那么计算容积，按照小的高度计算
                 * 1. 如果移动左指针，则最终高度可能变大，也可能变小，（因为取左右高度最小值）但是宽度一定变小，所以容积可能大，可能小
                 * 2. 如果移动右指针，则最终高度要么是现在左指针高度，要么比现在高度更小，而宽度一定变小，所以容积一定变小。
                 * 为了找出最大容积，所以要移动左指针
                 */
                left++;
            }else if(height[left] > height[right]){
                right--;
            }else {
                left++;
            }
            tmp = (right-left)*Math.min(height[left],height[right]);
            result = Math.max(result,tmp);
        }
        return result;
    }
    public static void main(String[] args) {
        int[] height = {1,8,6,2,5,4,8,3,7};
        int[] height1 = {3, 1, 2, 4, 5};
        System.out.println(maxArea1(height));
        System.out.println(maxArea(height));
    }
}
